
package com.eworking.eim;

import com.eworking.eim.util.EIMLog;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

/**
 * 初始化时，读入应用 manifest 文件里面的相关设置
 * 
 * @author yangsong
 */
public class EIMConfig {
    
    /** 日志标签 */
    private  final String _TAG = EIMConfig.class.getSimpleName();
    
    /** 企业代码 */
    private  final String _CONFIG_EWORKING_ENTCODE = "EWORKING_ENTCODE";
    
    /** IM服务器地址 - 配置项 */
    private  final String _CONFIG_EWORKING_IM_ADDRESS = "EWORKING_IM_ADDRESS";
    
    /** IM服务器域名称 - 配置项 */
    private  final String _CONFIG_EWORKING_IM_DOMAIN = "EWORKING_IM_DOMAIN";
    
    /** 图片、附件、语音等存储地址-配置项 */
    private  final String _CONFIG_EWORKING_ARCHIVE_ADDRESS = "EWORKING_ARCHIVE_ADDRESS";
    
    /** IM服务器地址 */
    public static String IM_SERVER = "eworking.im";
    
    /** IM服务器域名称 */
    public static String DOMAIN = "eworking.im";
    
    /** IM服务器域后缀 */
    public static String DOMAIN_SUFFIX = "@eworking.im";
    
    /** IM群组服务域名 */
    public static String MUC_DOMAIN = "conferene.eworking.im";
    
    /** IM群组服务域后缀 */
    public static String MUC_DOMAIN_SUFFIX = "@conference.eworking.im";
    
    /** 图片、附件、语音等存储地址 */
    public static String ARCHIVE_SERVER = "archive.eworking.com";
    
    /** 企业代码 --- 每个企业一个代码 */
    private String entCode = null;
    
    /** 令牌 */
    public String AccessToken = null;
    
    private Context applicationContext = null;
    
    /** debug模式 */
    public static boolean debugMode = false;
    
    private static EIMConfig instance = null;
    
    public static EIMConfig getInstance() {
        if (instance == null)
            instance = new EIMConfig();
        return instance;
    }
    
    public boolean loadConfig(Context context) {
        this.applicationContext = context;
        // 应用程序包名
        String packetName = this.applicationContext.getPackageName();
        ApplicationInfo localApplicationInfo = null;
        try {
            // 获取应用程序的信息
            localApplicationInfo = this.applicationContext.getPackageManager().getApplicationInfo(packetName, 128);
        } catch (PackageManager.NameNotFoundException localNameNotFoundException) {
            EIMLog.e(_TAG, localNameNotFoundException.getMessage());
            return false;
        }
        // 获取AndroidManifest.xml中<meta-data>标签中定义的值
        Bundle bundle = localApplicationInfo.metaData;
        // 获取企业代码
        String key = bundle.getString(_CONFIG_EWORKING_ENTCODE);
        
        if ((key == null) && (this.entCode == null)) {
            Log.e(_TAG, "EASEMOB_APPKEY is not set in AndroidManifest file");
            throw new RuntimeException("必须在清单文件里填写正确的EWORKING_APPKEY");
        }
        this.entCode = key;
        
        EIMLog.i(_TAG, "EWORKING_ENTCODE is set to:" + this.entCode);
        
        // IM服务器地址
        String imAddress = bundle.getString(_CONFIG_EWORKING_IM_ADDRESS);
        if (imAddress != null) {
            IM_SERVER = imAddress;
        }
        // 图片、附件、语音等存储地址
        String archiveServer = bundle.getString(_CONFIG_EWORKING_ARCHIVE_ADDRESS);
        if (archiveServer != null) {
            ARCHIVE_SERVER = archiveServer;
        }
        // IM服务器域名称
        String chatDomain = bundle.getString(_CONFIG_EWORKING_IM_DOMAIN);
        if (chatDomain != null) {
            DOMAIN = chatDomain;
            DOMAIN_SUFFIX = "@" + chatDomain;
        }
        EIMLog.d(_TAG, " entcode:" + this.entCode + " IM服务器地址:" + IM_SERVER + " IM服务器域名称:" + DOMAIN);
        EIMLog.d(_TAG, "图片、附件、语音等存储地址:" + ARCHIVE_SERVER);
        return true;
    }
    
    /**
     * 获取IM服务器域名称
     * 
     * @return
     */
    public String getDomain() {
        return DOMAIN;
    }
    
    /**
     * 获取企业代码
     * 
     * @return
     */
    public String getEntCode() {
        return this.entCode;
    }
    
    /**
     * 获取访问令牌
     * 
     * @return
     */
    public String getAccessToken() {
        return AccessToken;
    }
    
    /**
     * 设置访问令牌
     * 
     * @param accessToken 访问令牌
     */
    public void setAccessToken(String accessToken) {
        AccessToken = accessToken;
    }
    
    public Context getApplicationContext() {
        return this.applicationContext;
    }
    
    /**
     * 获取网络图片，语音的文件的存储位置
     * 
     * @return
     */
    public String getStorageUrl() {
        return ARCHIVE_SERVER;
    }
}
